#include "solar/trunk/NLyricLabel.h"

NLyricLabel::NLyricLabel(int MaxVsb,
                          int VerticalSpacing,
                          std::pair<QFont, QFont> OptionFont,
                          std::pair<QColor, QColor> OptionColor,
                          QWidget *parent){
    setFont(OptionFont);
    setColor(OptionColor);
    RdoMap_Bool["NextPassState"] = false;
    RdoMap_Integer["Size-W"] = width();
    RdoMap_Integer["Size-H"] = height();
    RdoMap_Integer["MaxPreviwe"] = MaxVsb;
    RdoMap_Integer["VerticalGap"] = VerticalSpacing;
    setParent(parent);
}

void NLyricLabel::setLyricMap(QMap<QString, QString> LyricMap){
    RdoMap_Map["TotalMap"] = LyricMap;
    QStringList SortKeys = LyricMap.keys();
    if (SortKeys.isEmpty()){
        SortKeys.sort(Qt::CaseInsensitive);
        RdoMap_List["SortedKey"] = SortKeys;
    } else {
        RdoMap_List["MidSortedKey"] = {}; //重要代码，无歌词时清除历史截取区间记录，确保提示文本可以正常绘画出来
        setText("当前曲目无歌词");
    }
    update();
}


void NLyricLabel::resizeEvent(QResizeEvent *Event){
    RdoMap_Integer["Size-W"] = Event->size().width();
    RdoMap_Integer["Size-H"] = Event->size().height();
}

void NLyricLabel::setFont(std::pair<QFont, QFont> OptionFont){
    RdoMap_Font["LightFont"] = OptionFont.first;
    RdoMap_Font["DeepFont"] = OptionFont.second;
    QFontMetrics FirstMetrics(OptionFont.first);
    QFontMetrics SecondMetrics(OptionFont.second);
    RdoMap_Integer["LightMetrics-H"] = FirstMetrics.height();
    RdoMap_Integer["DeepMetrics-H"] = SecondMetrics.height();
    update();
}

void NLyricLabel::setColor(std::pair<QColor, QColor> OptionColor){
    RdoMap_Color["LightColor"] = OptionColor.first;
    RdoMap_Color["DeepColor"] = OptionColor.second;
}

void NLyricLabel::setText(QString Text){
    int SortKeyStep = 0;
    int MapIndex = 0;
    if (RdoMap_List["SortedKey"].contains(Text)){
            MapIndex = RdoMap_List["SortedKey"].indexOf(Text);
            if (MapIndex >= RdoMap_Integer["MaxPreviwe"] / 2){
                SortKeyStep = MapIndex - SortKeyStep / 2;
            } else {
                /*必须加该判断赋值，情景1: A已经播放只29行歌词，KeyStep为24，当歌曲切换时，dataformat初始索引变更为0，
                SetTextCount已经不满足大于MaxPreviwe // 2的情况，因此KeyStep不会重新赋值，依旧是24，截取的歌词也是
                [24，24+MaxPreviwe]]这个区间，直到dateformat索引大于MaxPreviwe/2时KeyStep重新赋值后才开始正常显示歌词，此前的
                dateformat对应歌词已丢失，解决办法: 当不满足上列条件时，讲KeyStep手动赋值为0，重新开始截取区间*/
                SortKeyStep = 0;
            }
            int start_index = SortKeyStep;
            int end_index = SortKeyStep + RdoMap_Integer["MaxPreviwe"];
            RdoMap_List["MidSortedKey"] = RdoMap_List["SortedKey"].mid(start_index, end_index);
            if (RdoMap_List["MidSortedKey"].contains(Text)){
                RdoMap_Integer["DeepOfIndex"] = RdoMap_List["MidSortedKey"].indexOf(Text);
            }
    } else {
        if (QRegularExpression("\\d+\\W*\\d+").match(Text).capturedTexts().isEmpty()){
            RdoMap_String["PlaintText"] = Text;
        }
    }
    update();
}

void NLyricLabel::paintEvent(QPaintEvent*){
    QPainter CaptainBrush(this);
    QFontMetrics FocusedTextFont(RdoMap_Font["DeepFont"]);
    int VerticalGap = RdoMap_Integer["VerticalGap"];
    int NFontMetrics =RdoMap_Integer["DeepMetrics-H"];
    int AFontMetrics = RdoMap_Integer["LightMetrics-H"];
    int WinW = RdoMap_Integer["Size-W"];
    int WinH = RdoMap_Integer["Size-H"];
    int MaskKeyListLen = RdoMap_List["MidSortedKey"].size();
    int VerticalSpecing = VerticalGap * MaskKeyListLen;
    int LightHight = AFontMetrics * (MaskKeyListLen - 1);
    int VerticalBegin = (WinH - LightHight - NFontMetrics - VerticalSpecing) / 2;
    if (!RdoMap_List["MidSortedKey"].isEmpty() &&
        !RdoMap_Map["TotalMap"].isEmpty()){
        int MatchAdd = 0;
        int metrics_height;
        int metrics_width;
        int horizontal_start;
        foreach(QString Lyric, RdoMap_List["MidSortedKey"]){
            QString DeepText = RdoMap_Map["TotalMap"][Lyric];
            if (MatchAdd == RdoMap_Integer["DeepOfIndex"]){
                CaptainBrush.setFont(RdoMap_Font["DeepFont"]);
                CaptainBrush.setPen(RdoMap_Color["DeepColor"]);
                metrics_height = NFontMetrics;
                metrics_width = FocusedTextFont.horizontalAdvance(DeepText);
                horizontal_start = (WinW - metrics_width) / 2;
            } else {
                CaptainBrush.setFont(RdoMap_Font["LightFont"]);
                CaptainBrush.setPen(RdoMap_Color["LightColor"]);
                metrics_height = AFontMetrics;
                metrics_width = FocusedTextFont.horizontalAdvance(DeepText);
                horizontal_start = (WinW - metrics_width) / 2;
            }
            /*起始坐标为(0, vertical_start)， 单行矩形为(integer_dict["Size-W"], metrics_height)居中*/
            CaptainBrush.drawText(horizontal_start, VerticalBegin ,DeepText);
            VerticalBegin += metrics_height + VerticalGap;
            MatchAdd++;
        }
    } else {
        QStringList keys = RdoMap_String.keys();
        if (keys.contains("PlaintText"))
        {
            CaptainBrush.setPen(QColor(qRgb(17,139,129)));
            CaptainBrush.setFont(RdoMap_Font["DeepFont"]);
            QString PlaintText = RdoMap_String["PlaintText"];
            int metrics_width = FocusedTextFont.horizontalAdvance(PlaintText);
            int horizontalpos = (WinW - metrics_width) / 2;
            int verticalpos = (WinH - NFontMetrics) / 2;
            CaptainBrush.drawText(horizontalpos, verticalpos, PlaintText);
        }
    CaptainBrush.end();
    }
}
